package com.zks.Listener;

import cn.hutool.core.io.IoUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import io.netty.channel.ChannelProgressiveFuture;
import io.netty.channel.ChannelProgressiveFutureListener;

import java.io.RandomAccessFile;

public class FileProgressiveFutureListener implements ChannelProgressiveFutureListener {

    private static final Log log = LogFactory.get();

    private RandomAccessFile raf;
    public FileProgressiveFutureListener(RandomAccessFile raf) {
        this.raf = raf;
    }

    @Override
    public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) throws Exception {
        log.debug("Transfer progress: {} / {}", progress, total);

    }

    @Override
    public void operationComplete(ChannelProgressiveFuture future) throws Exception {
        IoUtil.close(raf);
    }
    /**
     * 构建文件进度指示监听
     *
     * @param raf RandomAccessFile
     * @return 文件进度指示监听
     */
    public static FileProgressiveFutureListener build(RandomAccessFile raf) {
        return new FileProgressiveFutureListener(raf);
    }
}
